package top.kaoshanji.r2ch07f;

import org.springframework.aop.ClassFilter;
import org.springframework.aop.support.DynamicMethodMatcherPointcut;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * 代码清单-r2ch07f-251页a：定义一个动态切点，检查方法入参值
 * @author kaoshanji
 * ^_^
 * create time 2025/5/8 10:10
 */
public class GreetingDynamicPointcut extends DynamicMethodMatcherPointcut {

    private static List<String> specialClientList = new ArrayList<>();
    static {
        // 方法有效的值
        specialClientList.add("John");
        specialClientList.add("Tom");
    }

    @Override
    public ClassFilter getClassFilter() { // 对类进行静态切点检查
        return new ClassFilter() {
            @Override
            public boolean matches(Class<?> clazz) {
                System.out.println("调用getClassFilter()对 " + clazz.getName()+ " 做静态检查.");
                return Waiter.class.isAssignableFrom(clazz);
            }
        };
    }

    @Override
    public boolean matches(Method method, Class<?> targetClass) { // 对方法进行静态切点切点检查
        System.out.println("调用matches(method,clazz)对 " + targetClass.getName() + "." + method.getName() + "做静态检查.");
        return "greetTo".equals(method.getName());
    }

    @Override
    public boolean matches(Method method, Class<?> targetClass, Object[] args) { // 对方法进行动态切点检查
        System.out.println("调用matches(method,clazz)对 " + targetClass.getName() + "." + method.getName() + "做动态检查.");
        String clientName = (String) args[0];
        return specialClientList.contains(clientName);
    }
}
